package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AddReservationDto;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CountByTimeVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    ReservationMapper reservationMapper;

    LocalDateTime start = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
    LocalDateTime end = LocalDateTimeUtil.endOfDay(LocalDateTime.now());

    @Override
    public Long selectCancelCount(Long id) {
        Long count = reservationMapper.selectCancelCount(id, start, end);
        return count;
    }

    @Override
    public List<CountByTimeVo> selectCountByTime(Long time) {
        Instant instant = Instant.ofEpochMilli(time);
        LocalDateTime datetime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = datetime.format(formatter);
        LocalDateTime parse = LocalDateTime.parse(formattedDateTime);
        List<CountByTimeVo> vos = reservationMapper.selectCountByTime(parse);
        for (int i = 0; i < vos.size(); i++) {
            CountByTimeVo reservationCountBytimeVo = vos.get(i);
            Integer count = reservationCountBytimeVo.getCount();
            reservationCountBytimeVo.setCount(6 - count);
        }
        return vos;
    }

    @Override
    public void addReservation(AddReservationDto dto) {
        Long count = reservationMapper.selectCancelCount(dto.getCreateBy(), start, end);
        if (count > 3) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        Integer peopleCount = reservationMapper.selectCountByGivenTime(dto.getTime());
        if (peopleCount >= 6) {
            throw new BaseException(BasicEnum.RESERVATION_COUNT_UPPER_LIMIT);
        }
        Long userId = UserThreadLocal.getUserId();
        Reservation reservation = BeanUtil.copyProperties(dto, Reservation.class);
        reservation.setCreateTime(LocalDateTime.now());
        reservation.setUpdateTime(LocalDateTime.now());
        reservation.setStatus(0);
        reservation.setCreateBy(userId);
        reservationMapper.addReservation(reservation);
    }

    @Override
    public PageResponse<ReservationVo> selectAll(ReservationDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<ReservationVo> page = reservationMapper.selectAll(dto);
        return PageResponse.of(page, ReservationVo.class);
    }

    @Override
    public void cancelReservation(Long id) {
        reservationMapper.cancelReservation(id);
    }
}

